// PreferencesTaskPage.cpp : implementation file
//

#include "stdafx.h"
#include "todolist.h"
#include "PreferencesTaskPage.h"

#include "..\shared\Misc.h"

//#include <locale.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPreferencesTaskPage property page

IMPLEMENT_DYNCREATE(CPreferencesTaskPage, CPreferencesPageBase)

CPreferencesTaskPage::CPreferencesTaskPage() : 
   CPreferencesPageBase(CPreferencesTaskPage::IDD)
{
	//{{AFX_DATA_INIT(CPreferencesTaskPage)
	//}}AFX_DATA_INIT

}

CPreferencesTaskPage::~CPreferencesTaskPage()
{
}

void CPreferencesTaskPage::DoDataExchange(CDataExchange* pDX)
{
	CPreferencesPageBase::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPreferencesTaskPage)
	DDX_CBString(pDX, IDC_DAYSINONEWEEK, m_sDaysInWeek);
	DDX_CBString(pDX, IDC_HOURSINONEDAY, m_sHoursInDay);
	DDX_Check(pDX, IDC_LOGTIME, m_bLogTime);
	DDX_Check(pDX, IDC_LOGTASKSEPARATELY, m_bLogTasksSeparately);
	DDX_Check(pDX, IDC_EXCLUSIVETRACKING, m_bExclusiveTimeTracking);
	DDX_Check(pDX, IDC_ALLOWPARENTRACKING, m_bAllowParentTimeTracking);
	DDX_Check(pDX, IDC_TRACKNONSELECTEDTASKS, m_bTrackNonSelectedTasks);
	DDX_Check(pDX, IDC_TRACKSCREENSAVER, m_bTrackOnScreenSaver);
	DDX_Check(pDX, IDC_TRACKNONACTIVETASKLISTS, m_bTrackNonActiveTasklists);
	DDX_Check(pDX, IDC_TRACKHIBERNATED, m_bTrackHibernated);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPreferencesTaskPage, CPreferencesPageBase)
	//{{AFX_MSG_MAP(CPreferencesTaskPage)
	ON_BN_CLICKED(IDC_LOGTIME, OnLogtime)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPreferencesTaskPage message handlers

BOOL CPreferencesTaskPage::OnInitDialog() 
{
	CPreferencesPageBase::OnInitDialog();

	m_mgrGroupLines.AddGroupLine(IDC_TRACKGROUP, *this); 
	m_mgrGroupLines.AddGroupLine(IDC_TIMEGROUP, *this); 

	GetDlgItem(IDC_LOGTASKSEPARATELY)->EnableWindow(m_bLogTime);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

double CPreferencesTaskPage::GetHoursInOneDay() const
{
	double dHours = Misc::Atof(m_sHoursInDay);

	if (dHours <= 0 || dHours > 24)
		dHours = 8;

	return dHours;
}

double CPreferencesTaskPage::GetDaysInOneWeek() const
{
	double dDays = Misc::Atof(m_sDaysInWeek);

	if (dDays <= 0 || dDays > 7)
		dDays = 5;

	return dDays;
}

void CPreferencesTaskPage::OnLogtime() 
{
	UpdateData();
	
	GetDlgItem(IDC_LOGTASKSEPARATELY)->EnableWindow(m_bLogTime);

	CPreferencesPageBase::OnControlChange();
}

void CPreferencesTaskPage::LoadPreferences(const CPreferences& prefs)
{
	// load settings
	m_sDaysInWeek = prefs.GetProfileString("Preferences", "DaysInWeek", "5.00");
	m_sHoursInDay = prefs.GetProfileString("Preferences", "HoursInDay", "8.00");
	m_bLogTime = prefs.GetProfileInt("Preferences", "LogTime", TRUE);
	m_bLogTasksSeparately = prefs.GetProfileInt("Preferences", "LogTasksSeparately", FALSE);
	m_bExclusiveTimeTracking = prefs.GetProfileInt("Preferences", "ExclusiveTimeTracking", FALSE);
	m_bAllowParentTimeTracking = prefs.GetProfileInt("Preferences", "AllowParentTimeTracking", TRUE);
	m_bTrackNonActiveTasklists = prefs.GetProfileInt("Preferences", "TrackNonActiveTasklists", TRUE);
	m_bTrackNonSelectedTasks = prefs.GetProfileInt("Preferences", "TrackNonSelectedTasks", TRUE);
	m_bTrackOnScreenSaver = prefs.GetProfileInt("Preferences", "TrackOnScreenSaver", TRUE);
	m_bTrackHibernated = prefs.GetProfileInt("Preferences", "AllowTrackingWhenHibernated", FALSE);

//	m_b = prefs.GetProfileInt("Preferences", "", FALSE);
}

void CPreferencesTaskPage::SavePreferences(CPreferences& prefs)
{
	// save settings
	prefs.WriteProfileInt("Preferences", "TrackNonSelectedTasks", m_bTrackNonSelectedTasks);
	prefs.WriteProfileInt("Preferences", "TrackNonActiveTasklists", m_bTrackNonActiveTasklists);
	prefs.WriteProfileInt("Preferences", "TrackOnScreenSaver", m_bTrackOnScreenSaver);
	prefs.WriteProfileInt("Preferences", "AllowTrackingWhenHibernated", m_bTrackHibernated);
	prefs.WriteProfileInt("Preferences", "LogTime", m_bLogTime);
	prefs.WriteProfileInt("Preferences", "LogTasksSeparately", m_bLogTasksSeparately);
	prefs.WriteProfileInt("Preferences", "ExclusiveTimeTracking", m_bExclusiveTimeTracking);
	prefs.WriteProfileInt("Preferences", "AllowParentTimeTracking", m_bAllowParentTimeTracking);

	// validate time periods before writing
	m_sHoursInDay.Format("%.2f", GetHoursInOneDay());
	m_sDaysInWeek.Format("%.2f", GetDaysInOneWeek());
	prefs.WriteProfileString("Preferences", "DaysInWeek", m_sDaysInWeek);
	prefs.WriteProfileString("Preferences", "HoursInDay", m_sHoursInDay);

//	prefs.WriteProfileInt("Preferences", "", m_b);
}


